package com.zhuss.food.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.food.DeleteFoodBo;
import com.zhuss.bean.vo.food.food.DetailFoodVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.FoodDao;
import com.zhuss.food.db.entity.Food;
import com.zhuss.food.db.entity.Product;
import com.zhuss.util.JsonUtil;

/**
 * 删除餐品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("foodDelete")
public class FoodDelete extends AbstractProcessor<Object, DeleteFoodBo> implements Processor<Object, DeleteFoodBo> {

    @Autowired
    private FoodDao foodDao;
    
    @Autowired
	@Qualifier("foodRead")
	private FoodRead foodRead;
    
    @Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
    
    
    /**
     * 校验
     */
	@Override
    public void check(DeleteFoodBo bo) throws WpsException {
    	Long foodId = bo.getFoodId();
    	List<Product> list = productRead.listByFood(foodId);
    	if (!list.isEmpty()) {
    		throw new WpsException(ExceptionCode.不能删除绑定商品的餐品);
    	}
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteFoodBo param) throws WpsException {
    	Long foodId = param.getFoodId();
    	String name = ((DetailFoodVo) foodRead.detail(foodId)).getName();
    	
    	Food record = new Food();
    	record.setFoodId(foodId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	foodDao.delete(record);
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteFoodBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.餐饮.key());
    	record.setAction(LogActionEnum.餐品删除.key());
    	record.setTargetId(param.getFoodId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
